import Dexie, { type Table } from 'dexie';

import { DB_NAME } from './constant'
import { historyKeys } from './initHistory'
import { exchangeKeys } from './initExchange'
import { stockHistoryKeys } from './initStock'
import { netListKeys } from './initNet'
import type { HoldItem } from './initHold';
import type { PlatformItem } from './initPlatform'
import type { StrategyItem } from './initStrategy'
import type { HoldHistoryItem } from './initHistory'
import type { CategoryItem } from './initCategory'
import type { ExchangeItem } from './initExchange'
import type { StockHistory } from './initStock'
import type { NetItem } from '@/views/net/types'

export class AssetsDexie extends Dexie {
  platformList!: Table<PlatformItem>
  strategyList!: Table<StrategyItem>
  categoryList!: Table<CategoryItem>
  holdList!: Table<HoldItem>
  historyList!: Table<HoldHistoryItem>
  exchangeList!: Table<ExchangeItem>
  stockHistoryList!: Table<StockHistory>
  netList!: Table<NetItem>
  constructor() {
    super(DB_NAME)
    this.version(1.9).stores({
      platformList: '++id, name, code, remarks',
      strategyList: '++id, name, code, remarks',
      categoryList: '++id, code, name, parentId, level',
      holdList: '++id, name, code, type, strategyId, platformId, categoryId, holdNum, holdPrice, createTime, updateTime, latestPrice, priceTime, riseAndFall, holdValue, holdEarnings, holdEarningsPercent',
      historyList: historyKeys,
      exchangeList: exchangeKeys,
      stockHistoryList: stockHistoryKeys,
      netList: netListKeys
    })
  }
}

export const assetsDB = new AssetsDexie()